package com.lianbang.staff.module.main

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.ubest.mvvmcore.base.BaseActivity
import cn.ubest.mvvmcore.base.BaseFragment
import cn.ubest.mvvmcore.utils.ResourcesUtils
import cn.ubest.mvvmcore.utils.SpanUtils
import cn.ubest.mvvmcore.widget.ListDivider
import com.google.android.material.appbar.AppBarLayout
import com.lianbang.staff.R
import com.lianbang.staff.adapter.HomeListAdapter
import com.lianbang.staff.bean.HomeDetailBean
import com.lianbang.staff.databinding.FragmentHomeBinding
import com.lianbang.staff.dialog.AddOrderDialog
import com.lianbang.staff.dialog.TodayIncomeDialog
import com.lianbang.staff.vm.MainVM
import kotlinx.android.synthetic.main.fragment_home.*
import kotlin.math.abs

/**
 * Created by maoqi on 2020/12/28.
 */
class HomeFragment : BaseFragment<MainVM, FragmentHomeBinding>() {

    companion object {

        fun newInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    override fun initLayout(): Int {
        return R.layout.fragment_home
    }

    override fun initData(arguments: Bundle?) {
        mViewModel.mLiveData.observe(this, Observer<Any> {
            if (it is HomeDetailBean) {
                tv_order.text =
                    SpanUtils().append("${it.orderNum}")
                        .setForegroundColor(ResourcesUtils.getColor(R.color.C_5671FB))
                        .setFontSize(ResourcesUtils.getDimens(R.dimen.sp_26))
                        .setBold().append("个门店下单").create()
                tv_running_water.text = "${it.orderPrice}"
            }
        })
    }

    override fun initView(view: View) {
        btn_today_income.setOnClickListener {
            (activity as MainActivity).addFragment(TodayIncomeDialog.newInstance())
        }
        btn_add_order.setOnClickListener {
            (activity as MainActivity).addFragment(AddOrderDialog.newInstance())
        }

        et_input.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {

            }

            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                if (et_input2.text.toString() != s.toString()) {
                    et_input2.setText(s)
                }
            }
        })
        et_input2.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (et_input.text.toString() != s.toString()) {
                    et_input2.setText(s)
                }
            }
        })
        al_content.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { appBarLayout, verticalOffset ->
            when {
                verticalOffset == 0 -> {
                    //完全展开

                }
                abs(verticalOffset) >= appBarLayout.totalScrollRange -> {
                    //完全折叠
                    if (fl_input.visibility != View.VISIBLE) {
                        fl_input.visibility = View.VISIBLE
                    }
                }
                else -> {
                    //过渡
                    if (fl_input.visibility != View.INVISIBLE) {
                        fl_input.visibility = View.INVISIBLE
                    }
                }
            }
        })

        rv_list.layoutManager = LinearLayoutManager(activity)
        rv_list.addItemDecoration(
            ListDivider.Build()
                .setColor(R.color.C_F3F5F9)
                .setMarginLeft(R.dimen.dp_20)
                .setMarinRight(R.dimen.dp_20)
                .build()
        )
        rv_list.adapter = HomeListAdapter().run {
            setOnItemClickListener { _, _, _ ->
                ChatActivity.start(activity)
            }
            this
        }
    }

    override fun loadData() {
        mViewModel.getHomeDetail()
    }

}